﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PlayBox
{
    public partial class Register : Form
    {
        public String Username
        {
            get
            {
                return txtUser.Text;
            }
            set
            {
                txtUser.Text = value;
            }
        }

        public Register()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            btnOk.Enabled = false;
            btnCancel.Enabled = false;
            progressLogin.Visible = true;

            var t = new System.Threading.Tasks.Task(() =>
            {
                User.Register(txtUser.Text, txtPassword.Text);
                this.Invoke(new Action(RegisterFinished));
            });
            t.Start();
        }

        private void RegisterFinished()
        {
            if (!User.IsLoggedIn)
            {
                btnOk.Enabled = true;
                btnCancel.Enabled = true;
                progressLogin.Visible = false;
            }
            else
            {
                this.Close();
            }
        }

        private void txtUser_TextChanged(object sender, EventArgs e)
        {
            CheckValid();
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            CheckValid();
        }

        private void CheckValid ()
        {
            btnOk.Enabled = txtUser.TextLength > 0 && txtPassword.TextLength > 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
